package com.dyj.ads

import org.apache.spark.sql.expressions.Window
import org.apache.spark.sql.{DataFrame, SparkSession}

object ads_e_mz_ave7662_bxb {
  def main(args: Array[String]): Unit = {
    val ds: String = args(0)

    val sparkSession: SparkSession = SparkSession.builder()
      .appName("白细胞指标统计")
      .enableHiveSupport()
      .config("spark.sql.shuffle.partitions", 1)
      .getOrCreate()

    import sparkSession.implicits._
    import org.apache.spark.sql.functions._

    sparkSession.sql("use bigdata03_dws")

    val bxbDF: DataFrame = sparkSession.sql(
      s"""
        |select
        |baoGaoBianHao,
        |baiXiBao,
        |baoGaoRiQi,
        |bxb_max,
        |bxb_min,
        |bxb_avg
        |from
        |bigdata03_dws.dws_e_mz_ave7662niaochanggui
        |where
        |ds='${ds}'
        |""".stripMargin)

    bxbDF
      .withColumn("number", count($"baoGaoBianHao") over Window.partitionBy($"baoGaoBianHao"))
      .withColumn("e_fc", ($"baiXiBao" - $"bxb_avg") * ($"baiXiBao" - $"bxb_avg"))
      .withColumn("a_fc", sum($"e_fc").over())
      .withColumn("bxb_fc", $"a_fc" / $"number")
      .withColumn("bxb_bzc", sqrt($"bxb_fc"))
      .withColumn("bxb_ycgs", sum(when($"baiXiBao" > 10000 or ($"baiXiBao" < 4000), 1)).over())
      .select($"baoGaoBianHao", $"baiXiBao", $"baoGaoRiQi", $"bxb_max", $"bxb_min", $"bxb_avg", $"bxb_fc", $"bxb_bzc", $"bxb_ycgs")
      .write
      .format("csv")
      .save("/daas/motl/bigdata03/ads/ads_e_mz_ave7662_bxb/ds=" + ds)
  }

}
